<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单 -->
      <el-form
          :model="form"
          :rules="rules" ref="loginForm"
          label-width="0px"
          class="login_form">
        <el-form-item prop="username">
          <el-input
              v-model="form.username"
              prefix-icon="el-icon-user"
              placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="form.password"
              prefix-icon="el-icon-key"
              placeholder="请输入密码" type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" round style="background-color: lightcoral; border: 1px solid #eee"
                     @click="login()">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import userApi from "@/api/user";

export default {
  name: "LoginView",
  created() {
    document.title = '欢迎登录';
  },

  data() {
    return {
      form: {
        username:'admin',
        password: '123'
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名：", trigger: "blur" },
          {
            min: 2,
            max: 25,
            message: "长度在 2 到 25 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 1, max: 15, message: "长度在 1到 15 个字符", trigger: "blur" },
        ],
      },
    }
  },
  methods: {
    login() {
      this.$refs['loginForm'].validate(async valid => {

        if (valid){
          const {data: res} = await userApi.login( this.form)
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: "登录成功",
            });
            console.log(res.data.access_token)
            // 缓存用户信息
            sessionStorage.setItem("user", JSON.stringify(res.data.access_token));
            this.$router.push('/home');
          } else {
            this.$message({
              type: 'error',
              message: res.message,
            })
          }
        }
      });
    },
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: lightcoral;
  height: 100vh;
}
.login_box {
  width: 450px;
  height: 350px;
  background: #fff;
  border-radius: 3px;
  box-shadow: 0 0 10px #ddd; //阴影
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #938ad9;
  }
  .avatar_box img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }

}
.login_form {
  position: absolute;
  bottom: 5%;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
  background-color: #eee;
}

</style>